using System;
using GammaJul.ReSharper.GammaXaml.Psi;
using JetBrains.Annotations;
using JetBrains.Application.Progress;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Feature.Services.Bulbs;
using JetBrains.ReSharper.Feature.Services.Xml.Bulbs;
using JetBrains.ReSharper.Intentions.Extensibility;
using JetBrains.ReSharper.Psi.Xml.Tree;
using JetBrains.TextControl;
using JetBrains.Util;

namespace GammaJul.ReSharper.GammaXaml.ContextActions.Xml {

	/// <summary>
	/// A simple context action that removes a XML attribute.
	/// </summary>
	[ContextAction(Name = "Remove attribute", Group = "XML", Description = "Removes attribute", Priority = -5)]
	public class RemoveAttributeAction : ContextActionBase {
		private readonly XmlContextActionDataProvider _provider;

		public override string Text {
			get { return "Remove attribute"; }
		}

		protected override Action<ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress) {
			_provider.FindNodeAtCaret<IXmlAttribute>().Delete();
			return null;
		}

		public override bool IsAvailable(IUserDataHolder cache) {
			return _provider.FindNodeAtCaret<IXmlAttribute>() != null;
		}

		public RemoveAttributeAction([NotNull] XmlContextActionDataProvider provider) {
			if (provider == null)
				throw new ArgumentNullException("provider");
			_provider = provider;
		}
	}

}
